package 方法测试;

import java.util.HashMap;
import java.util.Map;

public class getorDefault用法 {
    public static void main(String[] args) {
        Map<String,Integer> map=new HashMap<>();
        String[] book={"i","have","an","apple","he","have","a","pen"};
        for (String s : book) {
            map.put(s,map.getOrDefault(s,0)+1);
        }
        System.out.println(map.get("have"));
        //如果key存在对应的value，则输出value，否则输出defaultValue
        int age=map.getOrDefault("zsq",20)       ;
        System.out.println(age);
    }
}
